﻿
(function () {

    appModule.controller('tenant.views.instructionsFiles.index', [
        '$scope', '$uibModal', 'uiGridConstants', 'abp.services.app.instructionsFiles',
        function ($scope, $modal, uiGridConstants, instructionsFilesService) {
            var vm = this;

            $scope.$on('$viewContentLoaded', function () {
                Metronic.initAjax();
            });

            vm.loading = false;
            vm.filterText = null;
            vm.curInstructionsFiles = null;
            vm.permissions = {
                create: abp.auth.hasPermission('Pages.Tenant.InstructionsFiles.Create'),
                edit: abp.auth.hasPermission('Pages.Tenant.InstructionsFiles.Update'),
                'delete': abp.auth.hasPermission('Pages.Tenant.InstructionsFiles.Delete')
            };
            
            var urlInstructionsFiles = "/api/services/app/instructionsFiles/";

            var mainGridDataSource = new kendo.data.DataSource({
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: urlInstructionsFiles + "KendoGetInstructionsFiless",
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.data;
                        }
                        return {};
                    },
                    total: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { type: "number" },
                        }
                    }
                }
            });

            vm.mainGridOptions = {
                dataSource: mainGridDataSource,
                columnMenu: true,
                reorderable: true,
                resizable: true,
                filterable: true,
                scrollable: true,
                selectable: "multiple",
                allowCopy: true,
                height: 650,
                change: function (e) {
                    var selectedRows = this.select();
                    for (var i = 0; i < selectedRows.length; i++) {
                         vm.curInstructionsFiles = this.dataItem(selectedRows[i]);
                    }
                },
                pageable: {
                    buttonCount: 5.0,
                    pageSize: 10,
                    pageSizes: [10, 20, 50, 100],
                    refresh: true
                },
                sortable: {
                    mode: "multiple"
                },
                messages: {
                    commands: {
                        excel: "导出"
                    }
                },
                columns: [ 
                    {
                        title: 'Id ',
                        field: 'id',
                        width: 100
                    },
                    {
                        title: 'InstructionsId ',
                        field: 'instructionsId',
                        width: 100
                    },
                    {
                        title: 'FIleId ',
                        field: 'fIleId',
                        width: 100
                    },
                ]
            };
            
            function openCreateOreditInstructionsFilesModal(instructionsFilesId) {
                var modalInstance = $modal.open({
                    templateUrl: '~/App/tenant/views/instructionsfiles/createOrEditInstructionsFiles.cshtml',
                    controller: 'tenant.views.instructionsfiles.createOrEditInstructionsFiles as vm',
                    backdrop: 'static',
                    resolve: {
                        instructionsFilesId: function () {
                            return instructionsFilesId;
                        }
                    }
                });

                modalInstance.result.then(function (result) {
                    vm.mainGridOptions.dataSource.read();
                });
            }
            vm.editInstructionsFiles = function () {
                openCreateOreditInstructionsFilesModal( vm.curInstructionsFiles.id);
            };

            vm.createInstructionsFiles = function () {
                openCreateOreditInstructionsFilesModal(null);
            };

            vm.deleteInstructionsFiles = function () {
            abp.message.confirm(
                   "是否要删除此条记录！",
                   function (isConfirmed) {
                       if (isConfirmed) {
                          if (vm.curInstructionsFiles != null &&vm.curInstructionsFiles.id > 0) {
                    instructionsFilesService.deleteInstructionsFiles({ id:vm.curInstructionsFiles.id }).success(function () {
                        abp.notify.info(app.localize('SavedSuccessfully'));
                        vm.mainGridOptions.dataSource.read();
                    }).finally(function () {

                    });
                }
                else
                {
                   alert("请选择要删除的行");
                } 
                       }
                   }
               );
            
                
            };

        }]);
})();
